using Tower.Data;
using Share;
using System.Collections.Generic;
using TableDef;

namespace Tower
{

    public class RoleManager : SingletonObject<RoleManager>
    {
        public class LevelData
        {
            // 这一级别所有的角色
            List<long> Levels = new List<long>();
            public long GetID()
            {
                return RandomTools.GetRandList<long>(Levels);
            }

            public void AddLevelID(long id)
            {
                Levels.Add(id);
            }
        }

        Dictionary<RuleDefine.ECamp, Dictionary<int, LevelData>> m_LevelData = new Dictionary<RuleDefine.ECamp, Dictionary<int, LevelData>>();

        public long GetRoleID(RuleDefine.ECamp camp, int level)
        {
            if (!m_LevelData.TryGetValue(camp, out var campData))
            {
                XKLog.LogError($"没有对应的阵营数据[{camp}]");
                return -1;
            }

            if (!campData.TryGetValue(level, out var levelData))
            {
                XKLog.LogError($"没有对应的等级数据[{level}]");
                return -1;
            }

            return levelData.GetID();
        }
        protected override void OnInit()
        {
            for (int i = 0; i < ResDataMgr<t_role>.Instance().GetResDataNumber(); i++)
            {
                var role = ResDataMgr<t_role>.Instance().GetResDataByIndex(i);
                var camp = (RuleDefine.ECamp)role.m_nCamp;
                var bornLevel = role.m_nBorn;

                // 如果阵营字典中不存在该阵营，创建新的阵营字典
                if (!m_LevelData.TryGetValue(camp, out var campData))
                {
                    campData = new Dictionary<int, LevelData>();
                    m_LevelData[camp] = campData;
                }

                // 如果该阵营的等级字典中不存在该等级，创建新的LevelData
                if (!campData.TryGetValue(bornLevel, out var levelData))
                {
                    levelData = new LevelData();
                    campData[bornLevel] = levelData;
                }

                // 添加角色ID到对应的LevelData
                levelData.AddLevelID(role.GetMainID());
            }
        }
    }

}
